package com.qiyou.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qiyou.dao.IActivityDao;
import com.qiyou.dao.IActivityInvitationDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.Activity;
import com.qiyou.entity.ActivityInvitation;

/**
 * 加载活动详情，提交新活动
 * 
 * @author 赛艳志
 */
@WebServlet(name = "ActivityInfo", urlPatterns = { "/ActivityInfo" })
public class ActivityInfoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String idstr = request.getParameter("id");
        long id = -1;

        try {
            id = Long.parseLong(idstr);
            if (id > 0) {
                IActivityDao activityDao = DaoFactory.getActivityDao();
                IActivityInvitationDao invitationDao = DaoFactory
                        .getIActivityInvitationDao();

                Activity activity = null;
                List<ActivityInvitation> invitations = null;

                activity = activityDao.getActivity(id);
                invitations = invitationDao.getInvitations(id);

                if (null != activity) {
                    request.setAttribute("activity", activity);
                    request.setAttribute("join", invitations == null ? 0
                            : invitations.size());
                    request.getRequestDispatcher("activityinfo.jsp").forward(
                            request, response);
                }
            }
        } catch (Exception e) {
            response.getWriter()
                    .print("<script language='javascript'>alert('请求的用户信息有误');history.go(-1);</script>");
        }
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    }

    @SuppressWarnings("unused")
    private void addActivity(Activity activity, HttpServletResponse response)
            throws IOException {
        IActivityDao activityDao = DaoFactory.getActivityDao();

        try {
            if (activityDao.doCreate(activity)) {
                response.sendRedirect("ActivityList");
            } else {
                response.getWriter()
                        .print("<script  language='javascript'>alert('请求的用户信息有误');history.go(-1);</script>");
            }
        } catch (SQLException e) {
            response.getWriter()
                    .print("<script  language='javascript'>alert('提交信息时出错');history.go(-1);</script>");
        }
    }
}
